package com.briup.cms.service;

import com.briup.cms.bean.Article;
import com.briup.cms.dao.ArticleMapper;
import com.briup.cms.exception.CustomerException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService{

    @Autowired
    private ArticleMapper mapper;

    @Override
    public Article findByIdWithCategoryAndUser(int id) throws CustomerException {
        Article article = mapper.findByIdWithCategoryAndUser(id);
        return article;
    }

    @Override
    public void saveOrUpdate(Article article) throws CustomerException {
        if(article.getArticleId()!=null){
            System.out.println("修改信息@@Article"+article);
            mapper.update(article);
        }
        else if(article.getArticleId()==null){
            article.setArticlePublishTime(new Date(System.currentTimeMillis()));
            System.out.println("新增信息@@Article"+article);
            mapper.save(article);
        }
    }

    @Override
    public PageInfo<Article> pageQueryArticleWithCategoryAndUser(int pageNum, int pageSize, String title, Integer status, Integer categoryId) throws CustomerException {
        PageHelper.startPage(pageNum, pageSize,true);
        System.out.println("...Article"+title);
        List<Article> list = mapper.pageQueryArticleWithCategoryAndUser(title,status,categoryId);
        PageInfo<Article> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void deleteById(int id) throws CustomerException {
        mapper.deleteById(id);
    }

    @Override
    public PageInfo<Article> pageQueryRecommend(int pageNum, int pageSize) throws CustomerException {
//        PageHelper.startPage(pageNum, pageSize,true);
        List<Article> list = mapper.pageQueryRecommend(pageNum,pageSize);
        PageInfo<Article> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void changeStatus(int id, int status) throws CustomerException {
        mapper.changeStatus(id,status);
    }
}
